﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;
using Syncfusion.DocIO.DLS;
using Syncfusion.Mvc.Grid;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using Syncfusion.XlsIO;
using Membership = System.Web.Security.Membership;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        private BD_Vacinas2Context db = new BD_Vacinas2Context();

        public ActionResult Index()
        {
           return View();
        }

  /*    [HttpGet]
        public ActionResult IndexSearch(string filter)
        {

            var pacientes = from p in db.tb_Paciente
                            //    from v in db.VACINAs
                            //     from va in db.VACINACAOs
                            //    from u in db.UNIDADEs 

                            join va in db.tb_Vacinacao on p.ID_Paciente equals va.ID_Paciente
                            join v in db.tb_Vacina on va.ID_Vacina equals v.ID_Vacina
                            join u in db.tb_UBS on va.ID_UBS equals u.ID_UBS
                            join f in db.tb_Funcionario on va.ID_Funcionario equals f.ID_Funcionario
                            join f1 in db.tb_Funcionario on u.ID_UBS equals f1.ID_Funcionario
                            where p.ID_Paciente == va.ID_Paciente

                            select new
                            {
                                NomePaciente = p.Nome_Paciente,
                                p.Data_Nascimento,
                                NomeVacina = v.Nome_Vacina,
                                va.Data_Vacinacao,
                                NomeUnivdade = u.Nome_UBS,
                                NomeFuncionario = f.Nome_Funcionario,
                                va.Motivo
                            }; 

            var pacientes = from s in db.tb_Paciente
                            select s;

            if (!string.IsNullOrEmpty(filter))
            {
                pacientes = pacientes.Where(s => s.Nome_Paciente.Contains(filter));
            }
            return View(pacientes.ToList());
        } */

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult IndexSearch1(string filter, bool Name, PagingParams args)
        {
            var pacientes = from s in db.tb_Vacinacao
                            select s;

            var pacientes1 = pacientes;

            pacientes1 = pacientes1.Where(s => s.tb_Paciente.Nome_Paciente.Equals(filter));

            var dep = from d in db.tb_Paciente
                      select d;

            TempData["filtertemp"] = filter;
            TempData["filtertemp1"] = filter;
            TempData["filtertemp2"] = filter;
            TempData["filtertemp3"] = filter;
            TempData["namePac"] = filter;
            TempData["boolPac"] = Name;

            if (string.IsNullOrEmpty(filter))
            {
                return View("Index");
            }

            if (!string.IsNullOrEmpty(filter) && Name == true)
            {
                pacientes = pacientes.Where(s => s.tb_Paciente.Nome_Paciente.Equals(filter));
            }

            if (!string.IsNullOrEmpty(filter) && Name == false)
            {
                pacientes = pacientes.Where(s => s.tb_Paciente.CPF.Equals(filter));
            }

            TempData["nameOrCpf"] = Name;

            ViewData["data1"] = pacientes1.ToList();
            return View("IndexSearch", pacientes.ToList());
            //return RedirectToAction("IndexSearch", "Home");
            //return pacientes.GridActions<tb_Vacinacao>();
        }

        [HttpPost]
        public JsonResult doesUserNameExist(Home ho, string name)
        {

            var home = from h in db.tb_Paciente
                       select h;

            var model = TempData["namePac"] as string;
            var modelbool = TempData["boolPac"] as bool?;

            if (name == "true")
            {
                home = home.Where(h => h.Nome_Paciente.Equals(ho.filter));
            }

            if (name == "false")
            {
                home = home.Where(h => h.CPF.Equals(ho.filter));
            }

            //home = home.Where(h => h.CPF.Equals(ho.filter));

            return Json(home.Any());
        }


        [HttpPost]
        public JsonResult doesNomeExist(Home ho, bool name)
        {

            var home = from h in db.tb_Paciente
                       select h;

            var model = TempData["namePac"] as string;
            var modelbool = TempData["boolPac"] as bool?;


            home = home.Where(h => h.Nome_Paciente.Equals(ho.filter));

            return Json(home.Any());
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult IndexSearch(PagingParams args)
        {
            var model = TempData["filtertemp"] as string;

                IEnumerable data = db.tb_Vacinacao.Where(s => s.tb_Paciente.CPF.Equals(model));
                return data.GridActions<tb_Vacinacao>();

            //IEnumerable data = db.tb_Vacinacao.Where(s => s.tb_Paciente.Nome_Paciente.Contains(model)).Take(200).ToList();
            //return data.GridActions<tb_Vacinacao>();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChildGrid(PagingParams args, int? ID_Paciente)
        {
            IEnumerable data = db.tb_Dependente.Where(c => c.ID_Paciente == ID_Paciente).ToList();
            return data.GridActions<tb_Dependente>();
        }


        public ActionResult ExportToExcel()
        {
            var model = TempData["filtertemp"] as string;

            var verifica = TempData["nameOrCpf"] as bool?;

            if (verifica == false)
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.CPF.Contains(model)).ToList();
                return pacientes.GridExportToExcel<tb_Vacinacao>("GridExcel.xlsx", ExcelVersion.Excel2010);
            }

            else
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.Nome_Paciente.Contains(model)).ToList();
                return pacientes.GridExportToExcel<tb_Vacinacao>("GridExcel.xlsx", ExcelVersion.Excel2010);
            }
        }

        public ActionResult ExportToWord(PagingParams args)
        {
            var model = TempData["filtertemp1"] as string;

            var verifica = TempData["nameOrCpf"] as bool?;

            if (verifica == false)
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.CPF.Contains(model)).ToList();
                return pacientes.GridExportToWord<tb_Vacinacao>("GridWord.doc", DocumentVersion.Word2007, args.ExportOption);
            }
            else
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.Nome_Paciente.Contains(model)).ToList();
                return pacientes.GridExportToWord<tb_Vacinacao>("GridWord.doc", DocumentVersion.Word2007, args.ExportOption);
            }
        }

        public ActionResult ExportToPDF(PagingParams args)
        {
            var model = TempData["filtertemp2"] as string;

            var verifica = TempData["nameOrCpf"] as bool?;

            if (verifica == false)
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.CPF.Contains(model)).ToList();
                return pacientes.GridExportToPDF<tb_Vacinacao>("GridPDF.pdf", PdfVersion.Version1_7, args.ExportOption);
            }

            else
            {
                var pacientes = db.tb_Vacinacao.Where(s => s.tb_Paciente.Nome_Paciente.Contains(model)).ToList();
                return pacientes.GridExportToPDF<tb_Vacinacao>("GridPDF.pdf", PdfVersion.Version1_7, args.ExportOption);
            }
        }


        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
